<?php

namespace fastphp\util;

/**
 * 日志操作
 */
class Log
{
    public static function dbLogWrite($data,$executionTime)
    {
        try {
            if (!is_dir(LOG_PATH)) {
                mkdir(LOG_PATH);
            }
            if (!is_dir(DB_LOG_PATH)) {
                mkdir(DB_LOG_PATH);
            }
            $fileName = DB_LOG_PATH . '/' . date('Y-m-d') . '.log';
            $fp = fopen($fileName, 'a+');
            if (is_writable($fileName)) {
                fwrite($fp, date("Y-m-d H:i:s") . "      " . $executionTime . "      " . $data . "\r\n");
            }
            fclose($fp);
        } catch (\Exception $e) {
            self::dbErrLogWrite($e->getMessage());
        }
    }

    public static function dbErrLogWrite($data)
    {
        if (!is_dir(LOG_PATH)) {
            mkdir(LOG_PATH);
        }
        if (!is_dir(LOG_PATH . '/err')) {
            mkdir(LOG_PATH . '/err');
        }
        if (!is_dir(LOG_PATH . '/err/db')) {
            mkdir(LOG_PATH .  '/err/db');
        }
        $fileName = LOG_PATH .  '/err/db/' . date('Y-m-d') . '.log';
        $fp = fopen($fileName, 'a+');
        if (is_writable($fileName)) {
            fwrite($fp, date("Y-m-d H:i:s") . "      " . $data . "\r\n");
        }
        fclose($fp);
    }
}
